package com.cw.jeeyt.controller.mdata;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;

import org.springframework.stereotype.Controller;

import com.cw.jeeyt.api.dto.response.JsonResultDto;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;

import com.cw.jeeyt.service.bo.mdata.MdataAreaBo;
import com.cw.jeeyt.service.intf.mdata.MdataAreaService;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.lang.common.utils.PageUtil;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

import lombok.extern.slf4j.Slf4j;

/**
 * 地区控制层
 *
 * @author admin
 * @date 2021-03-26 22:30:39
 */
@Controller
@RequestMapping("/mdataArea")
@Slf4j
public class MdataAreaController extends BaseController {
    @Resource
    private MdataAreaService mdataAreaService;

    /**
     * 跳转到地区列表查询页面
     *
     * @return
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() {

        ModelAndView mv = new ModelAndView("mdata/mdataArea_index");
        mv.addObject("permission", new PermissionDirective());
        mv.addObject("PageDict", new PageDictDirective());
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable datatable数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listMdataArea")
    public ResultData listMdataArea(@RequestBody DataTable dataTable) {

        MdataAreaBo mdataAreaBo = JSONObject.parseObject(JSON.toJSONString(dataTable.queryCond), MdataAreaBo.class);
        if (mdataAreaBo == null) {
            mdataAreaBo = new MdataAreaBo();
        }

        PageParam pageParam = new PageParam();
        pageParam.setPageNum(PageUtil.calPageNum(dataTable.getStart(), dataTable.getLength()));
        pageParam.setPageSize(dataTable.getLength());
        PageInfo<MdataAreaBo> pageInfo = mdataAreaService.selectPage(mdataAreaBo, pageParam);
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList()) ? pageInfo.getList() : new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到地区表单页面
     *
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView editEdit(String areaId, String operation) {

        ModelAndView mv = new ModelAndView("mdata/mdataArea_form");
        MdataAreaBo mdataAreaBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            mdataAreaBo = new MdataAreaBo();
        } else {
            mdataAreaBo = mdataAreaService.selectByKey(areaId);
        }
        mv.addObject("mdataArea", mdataAreaBo);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 根据操作类型保存地区数据
     *
     * @param mdataAreaBo
     * @param operation add 新增  edit 修改
     */
    @ResponseBody
    @RequestMapping(value = "/saveMdataArea", method = RequestMethod.POST)
    public JsonResultDto saveMdataArea(MdataAreaBo mdataAreaBo, String operation) {

        JsonResultDto jsonResult = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                mdataAreaService.insert(mdataAreaBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                mdataAreaService.modify(mdataAreaBo);
            }
            jsonResult.setMsg("保存成功");
            jsonResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("保存失败:" + e.getMessage());
        }
        return jsonResult;
    }

    /**
     * 删除地区
     *
     * @param mdataAreaBo
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto delete(MdataAreaBo mdataAreaBo) throws Exception {

        JsonResultDto jsonResult = new JsonResultDto(false);
        try {
            mdataAreaService.delete(mdataAreaBo);
            jsonResult.setMsg("删除成功");
            jsonResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("删除失败:" + e.getMessage());
        }
        return jsonResult;
    }
    /**
     * 跳转到导入接口文档页面
     *
     * @return
     */
    @RequestMapping(value = "/to-importMDataAreaPage")
    public ModelAndView toImportExamQuestion() {
        ModelAndView mv = new ModelAndView("mdata/mdataArea_importForm");
        return mv;
    }
    /**
     * 导入题目
     *
     * @param request
     */
    @ResponseBody
    @RequestMapping(value = "/importMdataArea", method = RequestMethod.POST)
    public JsonResultDto importMdataArea(HttpServletRequest request) {
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            mdataAreaService.importMDataArea(request);
            jsonResultDto.setMsg("导入行政区划信息成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            jsonResultDto.setMsg("导入行政区划信息失败：" + e.getMessage());

            logger.error("导入行政区划信息失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 下载行政区划json
     *
     */
    @RequestMapping(value = "/downloadJsonData")
    public void downloadJsonData(HttpServletRequest request, HttpServletResponse response) {
        try {
            mdataAreaService.downloadJsonData(request,response);
            logger.info("下载行政区划json成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("下载行政区划json失败：" + e.getMessage());
        }
    }
}
